package com.example.springboot01.controller;

import com.example.springboot01.entity.Professor;
import com.example.springboot01.resp.enums.CommonRespEnum;
import com.example.springboot01.resp.enums.ProfessorRespEnum;
import com.example.springboot01.resp.Response;
import com.example.springboot01.service.ProfessorService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/professor")
public class ProfessorController {

    @Resource
    private ProfessorService professorService;

    @PostMapping
    public Response add(@RequestBody Professor professor){
        professorService.save(professor);
        return Response.ok(null);
    }

    @PutMapping
    public Response update(@RequestBody Professor professor){
        professorService.updateById(professor);
        return Response.response(CommonRespEnum.UPDATE_OK);
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id){
        if (professorService.removeProfessor(id)) {
            Response.response(CommonRespEnum.DELETE_OK);
        }
        return Response.response(ProfessorRespEnum.DELETE_ERROR);
    }

    @GetMapping
    public Response list(){
        return Response.ok(professorService.list());
    }


}
